// Copyright (c) 2009 Hypirinha contributers
// This program is made available under the terms of the MIT License.
package org.hypirinha.html.codegen;

import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.hypirinha.html.codegen.CompilationUnit.LICENCE;

import java.io.PrintStream;
import java.io.ByteArrayOutputStream;

public class GeneratedClassTest {

    @Test
    public void shouldProvideRelativePathIncludingPackageNameAndClassName() {
        CompilationUnit generatedClass = new GeneratedClass("org.hypirinha.html.elements", "Html");

        assertEquals("org/hypirinha/html/elements/Html.java", generatedClass.getFileName());
    }

    @Test
    public void shouldPrintPackageNameAtTheTopOfTheFile() {
        CompilationUnit generatedClass = new GeneratedClass("org.hypirinha.html.elements", "Html");

        assertTrue(print(generatedClass).startsWith(LICENCE + "package org.hypirinha.html.elements;\n"));
    }

    @Test
    public void shouldPrintImports() {
        CompilationUnit generatedClass = new GeneratedClass("org.hypirinha.html.elements", "Html");

        generatedClass.addImport("org.hypirinha.html.elements.contents.*");

        assertTrue(print(generatedClass).contains("\n\nimport org.hypirinha.html.elements.contents.*;\n"));
    }

    @Test
    public void shouldIncludeInterfaceDeclaration() {
        CompilationUnit generatedClass = new GeneratedClass("org.hypirinha.html.elements", "Html");

        assertTrue(print(generatedClass).contains("public class Html {\n"));
        assertTrue(print(generatedClass).endsWith("}\n"));
    }

    @Test
    public void shouldIncludeSuperInterface() {
        GeneratedClass generatedClass = new GeneratedClass("org.hypirinha.html.elements", "Body");

        generatedClass.setSuperClass("HtmlContent");
        assertTrue(print(generatedClass).contains("public class Body extends HtmlContent {\n"));
    }

    @Test
    public void shouldIncludeMultipleSuperInterfaces() {
        GeneratedClass generatedClass = new GeneratedClass("org.hypirinha.html.elements", "Tr");

        generatedClass.addInterface("TableContent");
        generatedClass.addInterface("TbodyContent");

        assertTrue(print(generatedClass).contains("public class Tr implements TableContent, TbodyContent {\n"));
    }

    @Test
    public void shouldIncludeMembers() {
        CompilationUnit generatedClass = new GeneratedClass("org.hypirinha.html.elements", "Html");

        generatedClass.getMembers().add(new Member() {
            public void printTo(PrintStream printStream) {
                printStream.println("This is a member");
            }
        });

        assertTrue(print(generatedClass).endsWith("{\nThis is a member\n}\n"));
    }

    private String print(CompilationUnit generatedClass) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        generatedClass.printTo(new PrintStream(outputStream));
        return new String(outputStream.toByteArray());
    }
}
